/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tela;

import gradehorario.Comprador;
import gradehorario.Item;
import gradehorario.Lance;
import gradehorario.Leilao;
import java.util.HashMap;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Marcio
 */
public class TelaLeilao extends javax.swing.JFrame {

    private Leilao leilao;
    private int indexTabLeiloeiroSelecionada;
    private int indexTabCompradorSelecionada;
    private Comprador compradorSelecionado;
    private Item itemSelecionado;
    /**
     * Creates new form TelaLeilao
     */
    public TelaLeilao() {
        initComponents();
        
        leilao = new Leilao(1);
        
        ChangeListener changeListenerTabLeiloeiro = new ChangeListener() 
        {
            public void stateChanged(ChangeEvent changeEvent) 
            {
                try 
                {
                    JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
                    indexTabLeiloeiroSelecionada = sourceTabbedPane.getSelectedIndex();

                    if(indexTabLeiloeiroSelecionada == 1)
                    {
                        atualizaTabLeilao();
                    }
                }                
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        };
        
        jtpLeiloeiro.addChangeListener(changeListenerTabLeiloeiro);
        
        ChangeListener changeListenerTabComprador = new ChangeListener() 
        {
            public void stateChanged(ChangeEvent changeEvent) 
            {
                try 
                {
                    JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
                    indexTabCompradorSelecionada = sourceTabbedPane.getSelectedIndex();

                    if(indexTabCompradorSelecionada == 1)
                    {
                        atualizaTabDetalheComprador();
                    }
                }                
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        };
        
        jtpComprador.addChangeListener(changeListenerTabComprador);
        
        setMensagemLog("Sistema iniciado com sucesso!");
    }

    private void atualizaTabLeilao()
    {
        if(!leilao.isLeilaoEmAndamento())
        {
            jcbItemSelecionado.removeAllItems();
            
            for(Item itemCadastrado : leilao.getItensCadastro())
            {
                jcbItemSelecionado.addItem(itemCadastrado);
            }
        }
    }
    
    private void atualizaTabDetalheComprador()
    {
        if(!leilao.isLeilaoEmAndamento())
        {
            jcbCompradorSelecionado.removeAllItems();
            
            for(Comprador comprador : leilao.getCompradores())
            {
                jcbCompradorSelecionado.addItem(comprador);
            }
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jtpLeiloeiro = new javax.swing.JTabbedPane();
        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfNomeItemCadastro = new javax.swing.JTextField();
        jtfValorItemCadastro = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblItensCadastrados = new javax.swing.JTable();
        jbAdicionarItemCadastro = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jcbItemSelecionado = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jtfValorItemSelecionado = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jtfValorLanceAtual = new javax.swing.JTextField();
        jtfNomeCompradorLanceAtual = new javax.swing.JTextField();
        jbLeiloar = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jtfNomeCompradorGanhador = new javax.swing.JTextField();
        jtfValorGanhador = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jtpComprador = new javax.swing.JTabbedPane();
        jPanel9 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jtfNomeCompradorCadastro = new javax.swing.JTextField();
        jbEntrarLeilao = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblCompradoresCadastro = new javax.swing.JTable();
        jPanel10 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jPanel11 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tblLancesComprador = new javax.swing.JTable();
        jScrollPane5 = new javax.swing.JScrollPane();
        tblItensCompradosComprador = new javax.swing.JTable();
        jcbCompradorSelecionado = new javax.swing.JComboBox();
        jtfCredito = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtaLog = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(new java.awt.GridLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Leiloeiro"));

        jLabel1.setText("Nome:");

        jLabel2.setText("Preço:");

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Itens"));

        tblItensCadastrados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Preço", "Leiloado"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Double.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblItensCadastrados);

        jbAdicionarItemCadastro.setText("Adicionar");
        jbAdicionarItemCadastro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAdicionarItemCadastroActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfNomeItemCadastro))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addComponent(jbAdicionarItemCadastro)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jtfValorItemCadastro))))
                .addContainerGap())
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfNomeItemCadastro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfValorItemCadastro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbAdicionarItemCadastro)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))
        );

        jtpLeiloeiro.addTab("Cadastro de Itens", jPanel5);

        jLabel3.setText("Ítem:");

        jcbItemSelecionado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione um ítem" }));
        jcbItemSelecionado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbItemSelecionadoActionPerformed(evt);
            }
        });

        jLabel4.setText("Valor:");

        jtfValorItemSelecionado.setEditable(false);

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("Lance Atual"));

        jLabel5.setText("Valor:");

        jLabel6.setText("Comprador:");

        jtfValorLanceAtual.setEditable(false);

        jtfNomeCompradorLanceAtual.setEditable(false);

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfNomeCompradorLanceAtual, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                    .addComponent(jtfValorLanceAtual))
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jtfValorLanceAtual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jtfNomeCompradorLanceAtual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbLeiloar.setText("Leiloar");
        jbLeiloar.setEnabled(false);
        jbLeiloar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbLeiloarActionPerformed(evt);
            }
        });

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("Ganhador"));

        jLabel7.setText("Comprador:");

        jLabel8.setText("Valor:");

        jtfNomeCompradorGanhador.setEditable(false);

        jtfValorGanhador.setEditable(false);

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfValorGanhador)
                    .addComponent(jtfNomeCompradorGanhador))
                .addContainerGap())
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jtfNomeCompradorGanhador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jtfValorGanhador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jcbItemSelecionado, 0, 291, Short.MAX_VALUE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel6Layout.createSequentialGroup()
                                .addComponent(jbLeiloar)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jtfValorItemSelecionado))))
                .addContainerGap())
            .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jcbItemSelecionado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtfValorItemSelecionado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbLeiloar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jtpLeiloeiro.addTab("Leilão", jPanel6);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jtpLeiloeiro)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jtpLeiloeiro)
        );

        jPanel1.add(jPanel3);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Compradores"));

        jLabel9.setText("Nome:");

        jbEntrarLeilao.setText("Entrar no Leilão");
        jbEntrarLeilao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEntrarLeilaoActionPerformed(evt);
            }
        });

        tblCompradoresCadastro.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Crédito"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(tblCompradoresCadastro);

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfNomeCompradorCadastro)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addComponent(jbEntrarLeilao)
                        .addGap(0, 179, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jtfNomeCompradorCadastro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbEntrarLeilao)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE))
        );

        jtpComprador.addTab("Cadastro de Compradores", jPanel9);

        jLabel10.setText("Comprador:");

        jLabel11.setText("Crédito:");

        jPanel11.setLayout(new java.awt.GridLayout(2, 1));

        jScrollPane4.setBorder(javax.swing.BorderFactory.createTitledBorder("Lances"));

        tblLancesComprador.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Ítem", "Valor Lance"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(tblLancesComprador);

        jPanel11.add(jScrollPane4);

        jScrollPane5.setBorder(javax.swing.BorderFactory.createTitledBorder("Itens Comprados"));

        tblItensCompradosComprador.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Ítem", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane5.setViewportView(tblItensCompradosComprador);

        jPanel11.add(jScrollPane5);

        jcbCompradorSelecionado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione um Comprador" }));
        jcbCompradorSelecionado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbCompradorSelecionadoActionPerformed(evt);
            }
        });

        jtfCredito.setEditable(false);

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcbCompradorSelecionado, 0, 260, Short.MAX_VALUE)
                    .addComponent(jtfCredito))
                .addContainerGap())
            .addComponent(jPanel11, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jcbCompradorSelecionado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jtfCredito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE))
        );

        jtpComprador.addTab("Detalhes Comprador", jPanel10);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jtpComprador)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jtpComprador))
        );

        jPanel1.add(jPanel4);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Informações"));

        jtaLog.setEditable(false);
        jtaLog.setColumns(20);
        jtaLog.setRows(5);
        jScrollPane1.setViewportView(jtaLog);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 720, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbAdicionarItemCadastroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAdicionarItemCadastroActionPerformed
        try 
        {
            String nomeItem = jtfNomeItemCadastro.getText();
            float valorItem = Float.valueOf(jtfValorItemCadastro.getText());
            
            Item itemCadastro = new Item(nomeItem, valorItem);
            leilao.addItemCadastro(itemCadastro);
            
            atualizaAbaCadastroItens();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }//GEN-LAST:event_jbAdicionarItemCadastroActionPerformed

    private void jcbItemSelecionadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbItemSelecionadoActionPerformed
        try
        {
            JComboBox cb = (JComboBox)evt.getSource();

            itemSelecionado = (Item)cb.getSelectedItem();

            if(itemSelecionado != null)
            {
                jtfValorItemSelecionado.setText(String.valueOf(itemSelecionado.getPreco()));
                jbLeiloar.setEnabled(true);
            }
            else
            {
                jbLeiloar.setEnabled(false);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }//GEN-LAST:event_jcbItemSelecionadoActionPerformed

    private void jbLeiloarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbLeiloarActionPerformed
        try 
        {            
            acoesDeTelaIniciarLeilao();
            leilao.iniciar(itemSelecionado);
            
            setMensagemLog("#### Leilão do Ítem " + itemSelecionado.getNome() + " iniciado!");
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }//GEN-LAST:event_jbLeiloarActionPerformed

    private void jbEntrarLeilaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEntrarLeilaoActionPerformed
        try 
        {
            String idComprador = jtfNomeCompradorCadastro.getText();
            
            Comprador comprador = new Comprador(idComprador);
            
            leilao.addComprador(comprador);
            
            atualizaAbaCadastroCompradores();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }//GEN-LAST:event_jbEntrarLeilaoActionPerformed

    private void jcbCompradorSelecionadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbCompradorSelecionadoActionPerformed
        
        try 
        {
            JComboBox cb = (JComboBox)evt.getSource();

            compradorSelecionado = (Comprador)cb.getSelectedItem();

            if(compradorSelecionado != null)
            {
                jtfCredito.setText(String.valueOf(compradorSelecionado.getCredito()));
                atualizaDetalhesComprador();
            }
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }//GEN-LAST:event_jcbCompradorSelecionadoActionPerformed

    private void atualizaDetalhesComprador()
    {
        DefaultTableModel modelTabelaLances =  (DefaultTableModel)tblLancesComprador.getModel();
        modelTabelaLances.getDataVector().clear();
        HashMap<Item, Lance> lances = compradorSelecionado.getLances();
        for(Item itemLance : lances.keySet())
        {
            Lance lance = lances.get(itemLance);
            Object[] linha = new Object[]{itemLance.getNome(), lance.getValor()};
            modelTabelaLances.addRow(linha);
        }
        
        DefaultTableModel modelTabelaItensComprados =  (DefaultTableModel)tblItensCadastrados.getModel();
        modelTabelaItensComprados.getDataVector().clear();
        List<Item> itensComprados = compradorSelecionado.getItensComprados();
        for(Item itemComprado : itensComprados)
        {
            Object[] linha = new Object[]{itemComprado.getNome(), 0};
            modelTabelaItensComprados.addRow(linha);
        }
        
        
    }
    
    private void acoesDeTelaIniciarLeilao()
    {
        jbAdicionarItemCadastro.setEnabled(false);
        jbLeiloar.setEnabled(false);
        jbEntrarLeilao.setEnabled(false);
        jcbItemSelecionado.setEnabled(false);
    }
    
    private void acoesDeTelaPararLeilao()
    {
        jbAdicionarItemCadastro.setEnabled(false);
        jbLeiloar.setEnabled(false);
        jbEntrarLeilao.setEnabled(false);
        jcbItemSelecionado.setEnabled(false);
    }
    
    
    
    private void atualizaAbaCadastroItens()
    {
        jtfNomeItemCadastro.setText("");
        jtfValorItemCadastro.setText("");
        
        DefaultTableModel model =  (DefaultTableModel)tblItensCadastrados.getModel();
        model.getDataVector().clear();
        for(Item itemCadastrado : leilao.getItensCadastro())
        {
            Object[] linha = new Object[]{itemCadastrado.getNome(), itemCadastrado.getPreco(), itemCadastrado.isLeiloado()};
            model.addRow(linha);
        }
    }
    
    private void atualizaAbaCadastroCompradores()
    {
        jtfNomeCompradorCadastro.setText("");
        
        DefaultTableModel model =  (DefaultTableModel)tblCompradoresCadastro.getModel();
        model.getDataVector().clear();
        for(Comprador comprador : leilao.getCompradores())
        {
            Object[] linha = new Object[]{comprador.getId(), comprador.getCredito()};
            model.addRow(linha);
        }
    }
    
    private void setMensagemLog(String mensagem)
    {
        String textoLog = jtaLog.getText();
        textoLog += mensagem + "\n";
        
        jtaLog.setText(textoLog);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaLeilao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaLeilao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaLeilao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaLeilao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaLeilao().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JButton jbAdicionarItemCadastro;
    private javax.swing.JButton jbEntrarLeilao;
    private javax.swing.JButton jbLeiloar;
    private javax.swing.JComboBox jcbCompradorSelecionado;
    private javax.swing.JComboBox jcbItemSelecionado;
    private javax.swing.JTextArea jtaLog;
    private javax.swing.JTextField jtfCredito;
    private javax.swing.JTextField jtfNomeCompradorCadastro;
    private javax.swing.JTextField jtfNomeCompradorGanhador;
    private javax.swing.JTextField jtfNomeCompradorLanceAtual;
    private javax.swing.JTextField jtfNomeItemCadastro;
    private javax.swing.JTextField jtfValorGanhador;
    private javax.swing.JTextField jtfValorItemCadastro;
    private javax.swing.JTextField jtfValorItemSelecionado;
    private javax.swing.JTextField jtfValorLanceAtual;
    private javax.swing.JTabbedPane jtpComprador;
    private javax.swing.JTabbedPane jtpLeiloeiro;
    private javax.swing.JTable tblCompradoresCadastro;
    private javax.swing.JTable tblItensCadastrados;
    private javax.swing.JTable tblItensCompradosComprador;
    private javax.swing.JTable tblLancesComprador;
    // End of variables declaration//GEN-END:variables
}
